/*
json
https://www.bilibili.com/video/BV1gf4y1r79E/?p=25&spm_id_from=pageDriver&vd_source=8bd7b24b38e3e12c558d839b352b32f4
*/
package main

import (
	"encoding/json"
	"fmt"
)

type Movie struct {
	Title  string   `json:"title"`
	Year   int      `json:"year"`
	Price  int      `json:"price"`
	Actors []string `json:"actors"`
}

func main() {
	movie := Movie{"喜剧之王", 2000, 10, []string{"周星驰", "张柏芝"}}

	// 编码过程 结构体->json
	jsonStr, err := json.Marshal(movie)
	if err != nil {
		fmt.Println("json marshal error", err)
		return
	}
	fmt.Printf("jsonStr = %s\n", jsonStr)

	// {"title":"喜剧之王","year":2000,"price":10,"actors":["周星驰","张柏芝"]}
	// 解码：json -> 结构体
	myMovie := Movie{}
	err = json.Unmarshal(jsonStr, &myMovie)
	fmt.Printf("%v\n", myMovie)
}
